<?php

namespace app\admin\controller;

use app\models\CampusModel;
use app\models\CompanyModel;
use app\models\DepartmentModel;
use app\models\TeacherModel;
use app\models\UserModel;

class Department extends Common
{

    /**
     * 用户管理模型.
     *
     *  @var UserModel
     */
    protected $userModel;

    /**
     * 部门管理模型.
     *
     * @var DepartmentModel
     */
    protected $departmentModel;

    /**
     * 教师管理模型.
     *
     * @var TeacherModel
     */
    protected $teacherModel;

	/**
	 * 集团管理模型.
	 *
	 * @var CompanyModel
	 */
	protected $companyModel;

    /**
     * 部门管理.
     *
     * @throws \think\exception\DbException
     *
     * @return mixed
     */
    public function lists()
    {
        if ($this->request->isAjax()) {
            $param = $this->request->param();
            $department = $this->departmentModel->getDepartmentLists($param);
            $this->success('success', '', $department, 0);
        }

        return $this->fetch('',
        	[
        		'type'=>get_current_type(),
				'company_id'=>get_current_company_id()
			]
		);
    }

	/**
	 * 选择集团.
	 *
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 *
	 * @return mixed
	 */
	public function company()
	{
		if ($this->request->isAjax()) {
			$company_id = input('company_id');
			$this->success('success', url('Department/department_add', ['company_id' => $company_id]), '', 0);
		}
		$where = [];
		$where['status'] = 1;

        /*************过滤掉未分配系统账号的集团*************/
        $user_result = $this->userModel->where(['wtype' => 2])->select();
        $company_user_arr = [];
        foreach ($user_result as $key => $value){
            $company_user_arr[] = $value['company_id'];
        }
        $company_user_arr = array_unique($company_user_arr);
        $company_user_str = implode(',',$company_user_arr);
        $where['id'] = ['in',$company_user_str];

		$company = $this->companyModel->where($where)->select();

		return $this->fetch('', [
			'company' => $company
		]);
	}

    /**
     * 添加部门.
     *
     * @return mixed
     */
    public function department_add()
    {
        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');
            if(get_current_type() != 1){
				$data['company_id'] = get_current_company_id();
			}
			if($this->departmentModel->get(['company_id' => $data['company_id'], 'department_name' => $data['department_name']])){
				$this->error('该部门已存在', null, '', 1);
			}
            if (false !== $this->departmentModel->save($data)) {
                $this->success('添加成功', url('Department/lists'), '', 1);
            }
            $this->error('添加部门失败', null, '', 1);
        }

        return $this->fetch('',['company_id' => input('company_id')]);
    }

    /**
     * 编辑部门.
     *
     * @return mixed
     */
    public function department_edit()
    {
        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');
			$data['company_id'] = get_current_company_id();
			if($this->departmentModel->get(['company_id' => $data['company_id'], 'department_name' => $data['department_name']])){
				$this->error('该部门已存在', null, '', 1);
			}
            if (!$this->teacherModel->where(['department_id' => $data['id']])->Update(['teacher_department' => $data['department_name']])){

                $this->error('修改教师表失败', null, '', 1);
            }
            $this->success('编辑成功', url('Department/lists'), '', 1);
        }
    }

    /**
     * 删除部门.
     *
     * @return mixed
     */
    public function department_del()
    {
        if ($this->request->isAjax() && $this->request->isPost()) {

            if (!$this->departmentModel->where(['id'=>input('id')])->delete()) {
                $this->error('删除部门失败', null, '', 1);
            }
            $this->success('删除成功', url('Department/lists'), '', 1);
        }
    }

    /**
     * 更改部门状态.
     *
     * @return mixed
     */
    public function department_status()
    {
        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');
            if($data['status'] == 1){
                $msg = '启用';
            }else{
                $msg = '禁用';
            }
            if (!$this->departmentModel->save($data,['id'=>$data['id']])) {
                $this->error($msg.'部门失败', null, '', 1);
            }
            $this->success($msg.'成功', '', '', 1);
        }
    }

    /**
     *  实例化.
     */
    protected function _initialize()
    {
        parent::_initialize();
        $this->departmentModel = new DepartmentModel();
        $this->teacherModel = new TeacherModel();
        $this->companyModel = new CompanyModel();
        $this->userModel = new UserModel();
    }
}
